package com.miao.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

//过滤器，为响应的http请求添加请求头
@Configuration
public class GlobleCorsConfig {

    @Bean
    public CorsFilter corsFilter(){
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedOrigin("*");//给所有的请求加请求头，允许它们跨域
        corsConfiguration.setAllowCredentials(true);//跨域允许发送cookie
        corsConfiguration.addAllowedHeader("*");//放行全部原始头信息
        corsConfiguration.addAllowedMethod("*");//表示允许所有的HTTP请求方法（例如GET、POST、PUT、DELETE等）进行跨域访问。


        UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();
        urlBasedCorsConfigurationSource.registerCorsConfiguration("/**", corsConfiguration);
        return new CorsFilter(urlBasedCorsConfigurationSource);
    }
}
